// 小程序开发api接口统一配置
//  
import{
  API_BASE_URL,
  POST_LOGIN,
  POST_UNLOCK_PERMISSION_CHECK,
  POST_LOCKINF_OUPLOAD,
  POST_LOCKMAC_REGISTER
}from '../http/url';
const request = (url, method, data) => {
    let _url = API_BASE_URL + url
    return new Promise((resolve, reject) => {
      wx.request({
        url: _url,
        method: method,
        data: data,
        header: {
          'Content-Type': 'application/json'
        },
        success(request) {
          resolve(request.data)
        },
        fail(error) {
          reject(error)
        },
        complete(com) {
          // 加载完成
        }
      })
    })
  }
  /**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}

  module.exports = {
    request,
    // 登陆
    postLogin: data => request(POST_LOGIN,'POST', data),
    // 查询蓝牙权限
    postUnlockPermissionCheck: (data) => request(POST_UNLOCK_PERMISSION_CHECK,'POST', data),
   //上传蓝牙锁信息
    postLockInfoUpload: (data) => request(POST_LOCKINF_OUPLOAD,'POST', data),
    //注册蓝牙mac
    postLockMacRegister: (data) => request(POST_LOCKMAC_REGISTER,'POST', data),
  }
  
